import { createRouter, createWebHistory } from 'vue-router'
import api from '@/utils/api'

// 懒加载组件
const Login = () => import('@/views/Login.vue')
const AdminLayout = () => import('@/views/admin/Layout.vue')
const POS = () => import('@/views/pos/Index.vue')
const Products = () => import('@/views/products/Index.vue')
const Orders = () => import('@/views/orders/Index.vue')
const Dashboard = () => import('@/views/admin/Dashboard.vue')
// 导入管理员版本的组件
const AdminProducts = () => import('@/views/admin/products/Index.vue')
const AdminCategories = () => import('@/views/admin/categories/Index.vue')
const AdminOrders = () => import('@/views/admin/orders/Index.vue')
const AdminUsers = () => import('@/views/admin/users/Index.vue')
const AdminDepartments = () => import('@/views/admin/departments/Index.vue')
const AdminRoles = () => import('@/views/system/RoleList.vue')
const Promotions = () => import('@/views/admin/promotions/PromotionManager.vue')
const Migrations = () => import('@/views/admin/migrations/Index.vue')
// 新报表系统
const Reports = () => import('@/views/admin/reports/Reports.vue')
// 库存管理组件 - 临时禁用
// const InventoryOverview = () => import('@/views/admin/inventory/Overview.vue')
// const InventoryWarehouse = () => import('@/views/admin/inventory/Warehouse.vue')
// const InventorySupplier = () => import('@/views/admin/inventory/Supplier.vue')
// const InventoryProduct = () => import('@/views/admin/inventory/Product.vue')
// 系统设置组件
const SystemSettings = () => import('@/views/system/SystemSettings.vue')
// const DockerLogs = () => import('@/views/admin/system/DockerLogs.vue')

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout.vue'),
    redirect: '/pos',
    children: [
      {
        path: 'pos',
        name: 'POS',
        component: POS,
        meta: { title: '收银系统' }
      },
      {
        path: 'products',
        name: 'Products',
        component: Products,
        meta: { title: '商品管理' }
      },
      {
        path: 'orders',
        name: 'Orders',
        component: Orders,
        meta: { title: '订单管理' }
      },
      {
        path: 'shifts',
        name: 'Shifts',
        component: () => import('@/views/shifts/Index.vue'),
        meta: { title: '日报管理' }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('@/views/settings/Index.vue'),
        meta: { title: '系统设置' }
      }
    ]
  },
  {
    path: '/admin',
    name: 'Admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'AdminHome',
        redirect: { name: 'Dashboard' }
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { requiresAuth: true, requiredRoles: ['cashier', 'manager', 'admin'] }
      },
      {
        path: 'products',
        name: 'AdminProducts',
        component: AdminProducts,
        meta: { requiresAuth: true, requiredRoles: ['manager', 'admin'], title: '商品管理' }
      },
      {
        path: 'categories',
        name: 'AdminCategories',
        component: AdminCategories,
        meta: { requiresAuth: true, requiredRoles: ['manager', 'admin'], title: '分类管理' }
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: AdminOrders,
        meta: { requiresAuth: true, requiredRoles: ['cashier', 'manager', 'admin'], title: '订单管理' }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: AdminUsers,
        meta: { requiresAuth: true, requiredRoles: ['admin'], title: '用户管理' }
      },
      {
        path: 'departments',
        name: 'AdminDepartments',
        component: AdminDepartments,
        meta: { requiresAuth: true, requiredRoles: ['admin'], title: '部门管理' }
      },
      {
        path: 'roles',
        name: 'AdminRoles',
        component: AdminRoles,
        meta: { requiresAuth: true, requiredRoles: ['admin'], title: '角色管理' }
      },
      {
        path: 'promotions',
        name: 'Promotions',
        component: Promotions,
        meta: { requiresAuth: true, requiredRoles: ['manager', 'admin'] }
      },
      {
        path: 'migrations',
        name: 'Migrations',
        component: Migrations,
        meta: { requiresAuth: true, requiredRoles: ['admin'], title: '数据迁移' }
      },

      {
        path: 'reports',
        name: 'Reports',
        component: Reports,
        meta: { requiresAuth: true, requiredRoles: ['manager', 'admin'], title: '报表中心' }
      },
      // 库存管理路由 - 临时禁用
      /* 
      {
        path: 'inventory',
        name: 'Inventory',
        meta: { title: '库存管理', icon: 'el-icon-s-goods', permissions: ['inventory.view'] },
        redirect: '/admin/inventory/overview',
        children: [
          {
            path: 'overview',
            name: 'InventoryOverview',
            component: () => import('@/views/admin/inventory/Overview.vue'),
            meta: { title: '库存概览', icon: 'el-icon-s-data', permissions: ['inventory.view'] }
          },
          {
            path: 'warehouse',
            name: 'Warehouse',
            component: () => import('@/views/admin/inventory/Warehouse.vue'),
            meta: { title: '仓库管理', icon: 'el-icon-office-building', permissions: ['inventory.warehouse.view'] }
          },
          {
            path: 'supplier',
            name: 'Supplier',
            component: () => import('@/views/admin/inventory/Supplier.vue'),
            meta: { title: '供应商管理', icon: 'el-icon-truck', permissions: ['inventory.supplier.view'] }
          },
          {
            path: 'product/:id',
            name: 'ProductInventory',
            component: () => import('@/views/admin/inventory/Product.vue'),
            meta: { title: '商品库存详情', icon: 'el-icon-goods', permissions: ['inventory.view'] },
            hidden: true
          },
          {
            path: 'stock-in',
            name: 'StockInList',
            component: () => import('@/views/admin/inventory/StockInList.vue'),
            meta: { title: '入库管理', icon: 'el-icon-box', permissions: ['inventory.stockIn.view'] }
          },
          {
            path: 'stock-in/create',
            name: 'CreateStockIn',
            component: () => import('@/views/admin/inventory/StockInForm.vue'),
            meta: { title: '新建入库单', icon: 'el-icon-plus', permissions: ['inventory.stockIn.create'] },
            hidden: true
          },
          {
            path: 'stock-in/edit/:id',
            name: 'EditStockIn',
            component: () => import('@/views/admin/inventory/StockInForm.vue'),
            meta: { title: '编辑入库单', icon: 'el-icon-edit', permissions: ['inventory.stockIn.edit'] },
            hidden: true
          },
          {
            path: 'stock-in/view/:id',
            name: 'ViewStockIn',
            component: () => import('@/views/admin/inventory/StockInDetail.vue'),
            meta: { title: '查看入库单', icon: 'el-icon-view', permissions: ['inventory.stockIn.view'] },
            hidden: true
          },
          // 出库管理路由
          {
            path: 'stock-out',
            name: 'StockOutList',
            component: () => import('@/views/admin/inventory/StockOutList.vue'),
            meta: { title: '出库管理', icon: 'el-icon-sell', permissions: ['inventory.stockOut.view'] }
          },
          {
            path: 'stock-out/create',
            name: 'CreateStockOut',
            component: () => import('@/views/admin/inventory/StockOutForm.vue'),
            meta: { title: '新建出库单', icon: 'el-icon-plus', permissions: ['inventory.stockOut.create'] },
            hidden: true
          },
          {
            path: 'stock-out/edit/:id',
            name: 'EditStockOut',
            component: () => import('@/views/admin/inventory/StockOutForm.vue'),
            meta: { title: '编辑出库单', icon: 'el-icon-edit', permissions: ['inventory.stockOut.edit'] },
            hidden: true
          },
          {
            path: 'stock-out/view/:id',
            name: 'ViewStockOut',
            component: () => import('@/views/admin/inventory/StockOutDetail.vue'),
            meta: { title: '查看出库单', icon: 'el-icon-view', permissions: ['inventory.stockOut.view'] },
            hidden: true
          },
          // 库存报表路由
          {
            path: 'reports',
            name: 'InventoryReports',
            component: () => import('@/views/admin/inventory/Reports.vue'),
            meta: { title: '库存报表', icon: 'el-icon-data-analysis', permissions: ['inventory.view'] }
          }
        ]
      },
      */
      {
        path: 'system-settings',
        name: 'SystemSettings',
        component: SystemSettings,
        meta: { requiresAuth: true, requiredRoles: ['admin'], title: '系统设置' }
      },
      // 渠道管理
      {
        path: 'channels',
        name: 'admin-channels',
        component: () => import('@/views/admin/channels/Index.vue'),
        meta: {
          title: '渠道管理',
          icon: 'Share',
          requiredPermission: 'channel_manage'
        }
      },
      {
        path: 'channels/:id/prices',
        name: 'admin-channel-prices',
        component: () => import('@/views/admin/channels/PriceManagement.vue'),
        meta: {
          title: '渠道价格管理',
          hideInMenu: true,
          requiredPermission: 'channel_manage'
        }
      },
      // 价格工具
      {
        path: 'price-tools',
        name: 'admin-price-tools',
        component: () => import('@/views/admin/channels/PriceTools.vue'),
        meta: {
          title: '价格管理工具',
          icon: 'PriceTag',
          requiredPermission: 'channel_manage'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: { title: '页面不存在' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (!token && to.path !== '/login') {
    // 未登录，访问非登录页，强制跳转到登录页
    return next('/login');
  }
  if (token && to.path === '/login') {
    // 已登录，访问登录页，跳转到主页面
    return next('/pos');
  }
  next();
});

export default router 

// 管理员路由 - 未使用，可以移除
// export const adminRoutes = [
//   {
//     path: '/admin',
//     component: () => import('@/layouts/AdminLayout.vue'),
//     meta: { requiredRoles: ['admin', 'manager', 'cashier'] },
//     children: [
//       // ... 其他子路由
//     ]
//   }
// ] 